package MULE.Model.Logic;

import MULE.Model.*;
import MULE.Model.Panel.*;

import java.awt.event.ActionEvent;
import java.util.EnumSet;

/**
 * Logic responsible for the store.
 * 
 * @author adityasehgal
 * 
 */
public class StoreLogic extends MulePresenter {

	public enum ActionCommand {
		ENTER_STORE, EXIT_STORE, EXCHANGE;
	}

	public enum ExchangeType {
		BUY, SELL
	}

	/**
	 * Constructor.
	 * 
	 * @param view
	 */
	public StoreLogic(StorePanel view) {
		super(view);
		view.addTransactionButtonListener(this);
		view.addExitButtonListener(this);
		view.updateAvailableResources(Store.getStorehouse());
	}

	/**
	 * Updates the store panel's available resources display.
	 */
	public void updateAvailableResources() {
		((StorePanel) view).updateAvailableResources(Store.getStorehouse());
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();

		if (command.equals(ActionCommand.ENTER_STORE.toString())) {
			// Store shows itself.
			updateAvailableResources();
			MuleDriver.setGameView(view);
		} else if (command.equals(ActionCommand.EXIT_STORE.toString())) {
			// Exit back to town.
			TownPanel townPanel = (TownPanel) MuleDriver.getTownLogic()
					.getView();
			MuleDriver.setGameView(townPanel);
		} else if (command.equals(ActionCommand.EXCHANGE.toString())) {
			// Make exchange:
			Player currentPlayer = MuleDriver.getGameLogic().getCurrentPlayer();
			// Ask panel for exchange type and quantity for each resource (e.g.
			// buy 50 food).
			for (Resource resource : EnumSet.allOf(Resource.class)) {
				if (resource == Resource.MONEY) {
					continue;
				}
				// Check transaction type.
				ExchangeType exchangeType = ((StorePanel) view)
						.getExchangeType(resource);
				int amount = ((StorePanel) view).getAmount(resource);
				if (amount == 0) {
					continue;
				}
				// Make transaction.
				if (exchangeType == ExchangeType.BUY) {
					if (Store.checkMoney(currentPlayer, resource, amount)) {
						// Check mules.
						if (resource == Resource.MULE) {
							if (currentPlayer.hasMule() || amount > 1) {
								MuleDriver.showAlertDialog("Store",
										"You can only own one mule!");
								return;
							}
						}
						if (Store.checkStorehouse(resource, amount)) {
							try {
								Store.buy(currentPlayer, resource, amount);
								((StorePanel) view)
										.updateAvailableResources(Store
												.getStorehouse());
							} catch (NotEnoughMoneyException e1) {
								MuleDriver.showAlertDialog("Store",
										"Not enough money to buy resources!");
							}

						} else {
							MuleDriver.showAlertDialog("Store",
									"Store does not have that many resources!");
						}

					} else {
						MuleDriver.showAlertDialog("Store",
								"Not enough money to buy resources!");
					}
				} else {
					if (Store.checkInventory(currentPlayer, resource, amount)) {
						Store.sell(currentPlayer, resource, amount);
						((StorePanel) view).updateAvailableResources(Store
								.getStorehouse());
					} else {
						MuleDriver.showAlertDialog("Store",
								"You don't have that many resources!");
					}
				}
				// Update scoreboard.
				MuleDriver.updateScore(currentPlayer);
			}
			((StorePanel) view).resetInterface();
		}
	}
}
